//
//  OrderDetailedViewController.m
//  JiongTu
//
//  Created by Dupengfei on 16/3/21.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "OrderDetailedViewController.h"
#import <AVQuery.h>
#import <AVInstallation.h>
#import <AVUser.h>
#import <SVProgressHUD.h>
#import "JTPassengerTrip.h"
#import "JTPushTool.h"
#import "JTUser.h"
#import "PopupView.h"
#import "LewPopupViewAnimationFade.h"
#import "UIViewController+LewPopupViewController.h"
@interface OrderDetailedViewController ()
@property (weak, nonatomic) IBOutlet UIVisualEffectView *effcet;
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *driverName;
@property (weak, nonatomic) IBOutlet UILabel *numberOfOver;
@property (weak, nonatomic) IBOutlet UIImageView *backgrundImage;
@property (weak, nonatomic) IBOutlet UILabel *aimLocation;
@property (weak, nonatomic) IBOutlet UIView *viewOfTrip;
@property (weak, nonatomic) IBOutlet UILabel *startLocation;
@property (weak, nonatomic) IBOutlet UILabel *endLocation;
//车牌
@property (weak, nonatomic) IBOutlet UILabel *plate;
@property (weak, nonatomic) IBOutlet UILabel *kindOfCar;
@property (weak, nonatomic) IBOutlet UIButton *orderCarButton;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIButton *passengerInfo;
@property (nonatomic, strong)UIBarButtonItem * rightButton;
@property (weak, nonatomic) IBOutlet UILabel *remark;
@property (nonatomic, strong)  JTUser * user;
@property (weak, nonatomic) IBOutlet UIButton *no2Button;
@property (weak, nonatomic) IBOutlet UIButton *no3Button;
@property (weak, nonatomic) IBOutlet UIButton *no4Button;

@end

@implementation OrderDetailedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeInterface];
    
    [self initializeDateSource];
    
}

-(void)initializeInterface
{
     self.title = @"订单详情";

//    [_plate sizeToFit];
    
    
    self.navigationItem.rightBarButtonItem =self.rightButton;
}

-(void)viewDidLayoutSubviews
{
    _headImage.layer.cornerRadius =_headImage.frame.size.width/2;
    
    _headImage.layer.masksToBounds = YES;
    
}


-(void)initializeDateSource
{
    NSArray * buttonArray =@[_passengerInfo,_no2Button,_no3Button,_no4Button];
    if (self.driverTrip.passenger.count !=0) {
        for (int i=0; i<self.driverTrip.passenger.count; i++) {
            UIButton * bu =[buttonArray[i] viewWithTag:2220+i];
            bu.layer.cornerRadius =bu.frame.size.width/2;
            bu.layer.masksToBounds = YES;
            JTPassengerTrip * passenger =self.driverTrip.passenger[i];
            JTUser *use =(JTUser *)passenger.passenger;
            [bu setBackgroundImage:[UIImage imageWithData:use.icon] forState:UIControlStateNormal];
            [bu addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    _startLocation.font = [UIFont systemFontOfSize:20];
    _endLocation.font = [UIFont systemFontOfSize:20];
    [_startLocation sizeToFit];
     _user = self.driverTrip.driver;
   _startLocation.text = self.driverTrip.startLocation;
    _endLocation.text = self.driverTrip.endLocation;
    _driverName.text = _user.nickname;
    _plate.text = _user.carNumber;
    _kindOfCar.text =_user.carName;
    _price.text =[NSString stringWithFormat:@"%@元/位",self.driverTrip.price];
    _aimLocation.text = [NSString stringWithFormat:@"目的地：%@",[_endLocation.text stringByAppendingString:self.driverTrip.endStreet]];
    _remark.text =self.driverTrip.remark;
    _remark.numberOfLines =0;
    [_remark sizeToFit];
    _numberOfOver.text =[NSString stringWithFormat:@"已完成订单数：%@单",_user.current];
    if (_user.icon ==nil) {
        _headImage.image = [UIImage imageNamed:@"头像占位"];
      _imageV.image =[UIImage imageNamed:@"头像占位"];
    }
    else
    {
        _headImage.image =[UIImage imageWithData:_user.icon];
        _imageV.image =[UIImage imageWithData:_user.icon];
    }
}

#pragma mark-交互事件
/**
 *  预约按钮！
 *
 *  @param sender 
 */
- (IBAction)orderButtonPressed:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    
    [JTPushTool pushToDriver:self.driverTrip withBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [SVProgressHUD showSuccessWithStatus:@"已告知司机,请到『我的订单』中支付相应订单"];
            AVObject *theDriverTrip = [AVObject objectWithoutDataWithClassName:@"DriverTrip" objectId:self.driverTrip.objectId];
            [theDriverTrip addUniqueObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"tempPassengerTripID"] forKey:@"Passenger"];
#warning 无法限制增加
            [theDriverTrip incrementKey:@"current" byAmount:[[NSUserDefaults standardUserDefaults] objectForKey:@"tempPassengerTripSeats"]];
            [theDriverTrip saveInBackground];
            AVObject *theTrip = [AVObject objectWithoutDataWithClassName:@"PassengerTrip" objectId:[[NSUserDefaults standardUserDefaults] objectForKey:@"tempPassengerTripID"]];
            AVUser *driver = [AVUser objectWithoutDataWithClassName:@"_User" objectId:self.driverTrip.driver.objectId];
            [theTrip setObject:driver forKey:@"DriverInformation"];
            [theTrip setObject:theDriverTrip forKey:@"DriverTripInformation"];
            //通知乘客时乘客的订单状态更改为已预约
            [theTrip setObject:@(1) forKey:@"IsOrdered"];
            [theTrip saveInBackground];
        }
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}
/**
 *  乘客按钮
 */
- (IBAction)passengerPressed:(UIButton *)sender {
    
}
- (IBAction)no2:(UIButton *)sender {
    
}

- (IBAction)no3:(UIButton *)sender {
    
    
}
- (IBAction)no4:(UIButton *)sender {
    
}




-(UIBarButtonItem *)rightButton
{
    if (!_rightButton) {
        _rightButton =[[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed:)];
    }
    return _rightButton;
    
}

-(void)rightButtonPressed:(UIBarButtonItem *)sender
{
    [self createObject];
    
}



-(void)buttonPressed:(UIButton *)sender
{
    PopupView *view = [PopupView defaultPopupView];
    view.parentVC = self;
    view.pass =self.driverTrip.passenger[sender.tag-2220];
    [self lew_presentPopupView:view animation:[LewPopupViewAnimationFade new] dismissed:^{
    }];

}


/**
 *  收藏交互事件
 */
-(void)createObject
{
    [SVProgressHUD showWithStatus:@"正在收藏"];
    
    AVObject * object =[AVObject objectWithoutDataWithClassName:@"_User" objectId:[AVUser currentUser].objectId];
    
//    [object setObject:_driver.objectId forKey:@"collection"];
    
//    [object addObject:self.driverTrip.objectId forKey:@"collection"];
    [object addUniqueObjectsFromArray:@[self.driverTrip.objectId] forKey:@"collection"];
    
    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
        NSLog(@"%@",error);
        if (succeeded) {
            [SVProgressHUD showSuccessWithStatus:@"收藏成功！"];
            
             _rightButton.title =@"已收藏";

        }
    }];
    
}

@end
